<html>
<head>
<style type="text/css">
.ui-datepicker-calendar {
    display: none;
    }</style>
<script type="text/javascript">
var aJaxURL 		= 'server-side/operations/letters.action.php';						//server side folder url
var tName			= "example-1";														//table name
var fName			= "add-edit-form";													//form name

$(document).ready(function () {

	LoadTable();

	$( document ).tooltip();
    $( "#radio" ).buttonset()
    $("input[name='radio']").on("change", function () {
       status=this.value;
       LoadTable();
    });
});

$(document).on("click", "#example-1 tbody tr", function () {

              $('#example-1 tbody tr').removeClass('mycolor');
              $(this).addClass('mycolor');

  });

function LoadTable(table,id){

	switch(table){
		case 'prod_list':
			var menuLength = [[100000], [1000000]];		
			GetDataTable2("prod_list", aJaxURL, "get_list", 4, "id="+id, 0, menuLength, '', '', [3]);
		    break;
		default:
			var menuLength = [[50], [50]];
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
            status="status="+status;
			GetDataTable1(tName, aJaxURL, "clients", 3, status, 0, menuLength,2, 'DESC');
			GetDataTable1("example-2", aJaxURL, "signed_all", 6, status, 0, menuLength);
			GetDataTable1("example-3", aJaxURL, "unsigned_all", 5, status, 0, menuLength);
			SetEvents("", "", "", "example-2", "inner-invoice-form" , aJaxURL);
	}

}

function LoadDialog(form,id, amount){
	switch(form){
	case 'inner-invoice-form' :
			var buttons = {
	        "print_invoce": {
                text: "ინვოისის ბეჭდვა",
                id: "print_invoce",
                click: function () {
               	 window.open("server-side/print/invoice.php?id="+$("#warehouseID").val(), "", "width=400, height=600");
                	
                	                
                }
            },
		        "send-rs": {
	                text: "ა/ფ  გაგზავნა",
	                id: "send-rs1",
	                click: function () {
		                
	                	var but = {
	                			"cancel": {
					                text: "ატვირთვა",
					                id: "ss",
					                click: function () {
					                	SendInerInvoice();
					                	$(this).dialog("close");
					                }
					            }
			                	};
	                	GetDialog("change_mont",'130','auto',but);
	                	 var d = new Date();
		                  var n = (d.getMonth()+1).toString();
		                  if(n.length==1){
		                	    n = "0"+ n;
			                  }
		                  $("#mont_id").val(n);	
	                    	                	
	                	
	                //	
	                }
	            },
		        "cancel": {
	                text: "დახურვა",
	                id: "cancel-dialog",
	                click: function () {
	                	$(this).dialog("close");
	                }
	            }
			};
			GetDialog(form, 600, "auto",buttons);
		break;
	default:
	    $.ajax({
	        url: aJaxURL,
		    data: "act=get_invoice_form&id="+id+"&amount="+amount,
	        success: function(data) {
				if(typeof(data.error) != "undefined"){
					if(data.error != ""){
						alert(data.error);
					}else{
						var buttons = {
						        "print_invoce": {
					                text: "ინვოისის ბეჭდვა",
					                id: "print_invoce",
					                click: function () {
					                	print_invoce()    ;                
					                }
					            },
						        "send-rs": {
					                text: "ა/ფ  გაგზავნა",
					                id: "send-rs",
					                click: function () {
						                
					                	var but = {
					                			"cancel": {
									                text: "ატვირთვა",
									                id: "ss",
									                click: function () {
									                	SendInvoice();
									                	$(this).dialog("close");
									                }
									            }
							                	};
					                	GetDialog("change_mont",'auto','auto',but);
					                	  var d = new Date();
						                  var n = (d.getMonth()+1).toString();
						                  if(n.length==1){
						                	    n = "0"+ n;
							                  }
						                  $("#mont_id").val(n);			                	
					                	
					                }
					            },
						        "cancel": {
					                text: "დახურვა",
					                id: "cancel-dialog",
					                click: function () {
					                	$(this).dialog("close");
					                }
					            }
							};
						$("#"+fName).html(data.page);
						GetDialog(fName, 600, "auto",buttons);
						LoadTable("prod_list", id);
					    $("#check-all").on("click", function () {
					    	$("#" + "prod_list" + " INPUT[type='checkbox']").attr("checked", $("#check-all").is(":checked"));
					    });
					}
				}
		    }
	    });
	}

}

function SendInerInvoice(){
	var month=$("#mont_id").val();
	 
	var invoice_aJaxURL ="server-side/wsdl/invoice.action.php";
	var act				= "create_invoice";
	var client_id 		= $("#client_id").val();
	var wareHouse_id	= $("#warehouseID").val();
    var wareHouseID = [];
    wareHouseID.push(wareHouse_id);


    param = new Object();
    //Action
    param.month	      = month;
    param.detail	  = $("#detail").is(':checked');
	param.act	      = act;
	param.client_id	  = client_id;
    param.wareHouseID = wareHouseID;

    var link	=  GetAjaxData(param);

    $.ajax({
        url: invoice_aJaxURL,
	    data: link,
	    dataType: "json",
        success: function(data) {
            if (data.error != "") {
            	alert(data.error);
            }
            $("#inner-invoice-form").dialog("close");
        	$("#inner-invoice-form").html("");
        	LoadTable();
	    }
    });
}
function SendInvoice(){
	var month=$("#mont_id").val();  
	var invoice_aJaxURL ="server-side/wsdl/invoice.action.php";
	var act				= "create_invoice";
	var client_id 		= $("#client_id").val();
    var data = $(".check:checked").map(function () { //Get Checked checkbox array
        return this.value;
    }).get();
    
    var wareHouseID = [];

    for (var i = 0; i < data.length; i++) {
    	wareHouseID.push(data[i]);
    }

    param = new Object();
    //Action
	param.act	      = act;
	param.detail	  = $("#detail").is(':checked');
	param.month	      = month;
	param.client_id	  = client_id;
    param.wareHouseID = wareHouseID;

    var link	=  GetAjaxData(param);

    $.ajax({
        url: invoice_aJaxURL,
	    data: link,
	    dataType: "json",
        success: function(data) {
            if (data.error != "") {
            	alert(data.error);
            	
            	$("#add-edit-form").dialog("close");
            	$("#add-edit-form").html("");
            	LoadTable();
            }
	    }
    });
}
function print_invoce(){
	data = new Object();
    data.id = $(".check:checked").map(function () {return this.value;}).get();    
    window.open("server-side/print/invoice.php?id="+data.id, "", "width=400, height=600");
    console.log(data);
	
}
</script>
</head>
<body>
    
  <br />
  <br />
  <br />
  
  <div id="radio">
    <input type="radio" id="radio2" value="0" name="radio"><label for="radio2">იურიდიული</label>   
    <input type="radio" id="radio1" value="1" name="radio"><label for="radio1">კერძო პირი</label> 
  </div>
	<table id="main-table">
		<tr>
			<td>
			    <div id="dt_example" class="ex_highlight_row">
			        <div id="container" style="width: 400px;">
			            <div id="dynamic">
			            	<h2 class="left">კლიენტები</h2>

			                <table class="display" id="example-1" style="width: 380px !important; ">
			                    <thead class="ui-widget-header">
			                        <tr style=" white-space: no-wrap;" id="datatable_header">
			                            <th >ID</th>
			                            <th style="width: 280px  !important;">კლიენტი</th>
			                            <th style="width: 100px !important;">თანხა</th>
			                        </tr>
			                    </thead>
			                </table>
			            </div>
			            <div class="spacer">
			            </div>
			        </div>
			    </div>
			</td>

			<td>
			    <div id="dt_example" class="ex_highlight_row">
			        <div id="container" style="width: 450px;">
			            <div id="dynamic" style="overflow-x: none !important;">
			            	<h2 class="left">ხელმოწერილი</h2>
			                <table class="display" id="example-2" style="width: 450px;">
			                    <thead>
			                        <tr id="datatable_header">
			                            <th>ID</th>
			                            <th style="width: 75px">ბარათის N</th>
			                            <th style="width: 79px" title="შეფუთვის  თარიღი">შეფუთვის  </br>თარიღი</th>
			                            <th style="width: 79px" title="მიტანის თარიღი" >მიტანის </br>თარიღი</th>
			                            <th style="width: 90px">ფილიალი</th>
			                            <th style="width: 70px">თანხა</th>
			                        </tr>
			                    </thead>
			                </table>
		                </div>
		            </div>
			    </div>
			</td>

			<td>
			    <div id="dt_example" class="ex_highlight_row">
			        <div id="container" style="width: 400px;">
			            <div id="dynamic">
			            	<h2 class="left">ხელმოსაწერი</h2>
			                <table class="display" id="example-3" style="width: 400px;">
			                    <thead>
			                        <tr id="datatable_header">
			                            <th>ID</th>
			                            <th style="width: 76px">ბარათის N</th>
			                            <th style="width: 90px" title="შეფუთვის თარიღი">თარიღი</th>
			                            <th style="width: 90px">ფილიალი</th>
			                            <th style="width: 85px">თანხა</th>
			                        </tr>
			                    </thead>
			                </table>
			            </div>
			            <div class="spacer">
			            </div>
			        </div>
			    </div>
			</td>

		</tr>
	</table> 
	<div id="add-edit-form" class="form-dialog" title="ინვოისი">
    	<!-- aJax -->
	</div>
	<div id="inner-invoice-form" class="form-dialog" title="ინვოისი">
    	<!-- aJax -->
	</div>
	<div id="change_mont" style="display: none" class="form-dialog" title="აირჩიეთ თვე">
	<select id="mont_id"> 
	<option value="01">ინვარი</option>
	<option value="02">თებერვალი</option>
	<option value="03">მარტი</option>
	<option value="04">აპრილი</option>
	<option value="05">მაისი</option>
	<option value="06">ივნისი</option>
	<option value="07">ივლისი</option>
	<option value="08">აგვისტო</option>
	<option value="09">სექტემბერი</option>
	<option value="10">ოქტომბერი</option>
	<option value="11">ნოემბერი</option>
	<option value="12">დეკემბერი</option>
	</select> 
    <br/>
    <br/>
    <br/>         
    <input  type="checkbox" id="detail" checked />  
    <label for="detail"> დეტალურად</label>  
	</div>
</body>
</html>